cmake_minimum_required(VERSION 3.14)

set(CMAKE_BUILD_TYPE "Debug" CACHE STRING
    "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)

project(hilange C CXX)
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})

set(HILANG_VERSION_MAJOR 0)
set(HILANG_VERSION_MINOR 0)
set(HILANG_VERSION_PATCH 0)
set(HILANG_VERSION "${HILANG_VERSION_MAJOR}.${HILANG_VERSION_MINOR}.${HILANG_VERSION_PATCH}")
message("Configuring hilange version ${HILANG_VERSION}")

set(LLVM_DIR "/usr/lib/llvm-15/cmake")
find_package(LLVM REQUIRED CONFIG)
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
include_directories(${LLVM_INCLUDE_DIR})
separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
add_definitions(${LLVM_DEFINITIONS_LIST})
#link_directories(${LLVM_LINK_DIR})

include_directories(
    ${CMAKE_SOURCE_DIR}
    ${CMAKE_BINARY_DIR}
)

set(GRAMMAR_TXT "${CMAKE_BINARY_DIR}/simple.txt")

set(HILANG_SOURCES
    "${CMAKE_SOURCE_DIR}/src/buffer.cpp"
    "${CMAKE_SOURCE_DIR}/src/error.cpp"
    "${CMAKE_SOURCE_DIR}/src/main.cpp"
    "${CMAKE_SOURCE_DIR}/src/parser.cpp"
    "${CMAKE_SOURCE_DIR}/src/tokenizer.cpp"
    "${CMAKE_SOURCE_DIR}/src/util.cpp"
    "${CMAKE_SOURCE_DIR}/src/codegen.cpp"
    "${CMAKE_SOURCE_DIR}/src/hilang_llvm.cpp"
    "${CMAKE_SOURCE_DIR}/src/os.cpp"
    "${CMAKE_SOURCE_DIR}/src/analyze.cpp"
)

set(TEST_SOURCES
    "${CMAKE_SOURCE_DIR}/src/buffer.cpp"
    "${CMAKE_SOURCE_DIR}/src/util.cpp"
    "${CMAKE_SOURCE_DIR}/src/os.cpp"
    "${CMAKE_SOURCE_DIR}/test/run_tests.cpp"
)

set(CONFIGURE_OUT_FILE "${CMAKE_BINARY_DIR}/config.h")
configure_file (
    "${CMAKE_SOURCE_DIR}/src/config.h.in"
    ${CONFIGURE_OUT_FILE}
)

include_directories(
    ${CMAKE_SOURCE_DIR}
    ${CMAKE_BINARY_DIR}
    "${CMAKE_SOURCE_DIR}/src"
)

# GTFO, -lstdc++ !!
set(CMAKE_CXX_IMPLICIT_LINK_LIBRARIES "")
set(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "")

set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Wno-unused-variable -Wno-unused-but-set-variable")

set(EXE_CFLAGS "-Werror -Wall -std=c++14")


add_executable(hilange ${HILANG_SOURCES})
set_target_properties(hilange PROPERTIES
    LINKER_LANGUAGE CXX
    COMPILE_FLAGS ${EXE_CFLAGS})

llvm_map_components_to_libnames(llvm_libs support core irreader object analysis target codegen 
    x86asmparser X86CodeGen
    AArch64AsmParser AArch64CodeGen
    AMDGPUAsmParser AMDGPUCodeGen
    ARMAsmParser ARMCodeGen
    AVRAsmParser AVRCodeGen
    BPFAsmParser BPFCodeGen
    HexagonAsmParser HexagonCodeGen
    LanaiAsmParser LanaiCodeGen
    MIPSAsmParser MIPSCodeGen
    PowerPCAsmParser PowerPCCodeGen
    MSP430AsmParser MSP430CodeGen
    RISCVAsmParser RISCVCodeGen
    SparcAsmParser SparcCodeGen
    SystemZAsmParser SystemZCodeGen
    VEAsmParser VECodeGen
    XCoreCodeGen
    NVPTXCodeGen
    WebAssemblyAsmParser WebAssemblyCodeGen
    M68kAsmParser M68kCodeGen
    )

message("LLVM libs is ${LLVM_LIBRARIES}")

target_link_libraries(hilange LINK_PUBLIC
    ${llvm_libs}
)
install(TARGETS hilange DESTINATION bin)

add_executable(run_tests ${TEST_SOURCES})
target_link_libraries(run_tests)
set_target_properties(run_tests PROPERTIES
    COMPILE_FLAGS ${EXE_CFLAGS}
)

